Display = {};

local referenceCount = {};

function Display:ctor(...)
    self.module = nil;
    self.textureDic = {};
    self.data = nil;
    self.isInit = false;
    self.prefabPath = nil;
    self.parent = nil;
    self.gameObject = nil;
    self.visible = true;
    self.messageDic = {};
    self.rtf = nil;
    self:init(...);
end

function Display:init(...)

end

function Display:onStart()

end

function Display:onShow()

end

function Display:onHide()
    
end

function Display:onDestroy()

end

function Display:onData()

end

function Display:setData(data)
    self.data = data;
    if self.isInit and self.visible then
        self:onData();
    end
end

--handler 必须是匿名函数
function Display:addListener(type, handler)
    if self.messageDic[type] == nil then
        self.messageDic[type] = {};
    end
    table.insert(self.messageDic[type], handler);
    Dispatcher:addMessageListener(type, handler);
end

function Display:removeListener()
    for k, v in pairs(self.messageDic) do
        local handlers = self.messageDic[k];
        for i, h in ipairs(handlers) do
            Dispatcher:removeMessageListener(k, h);
            handlers[i] = nil;
        end
    end
end

function Display:loadGameObject(gameObject)
    self.gameObject = gameObject;
    self.isInit = true;
    self:loadPrefabComplete();
end

function Display:loadPrefab(prefabPath, parent)
    if self.isInit then
        return ;
    end
    if parent ~= nil then
        self.parent = parent;
    end
    if prefabPath ~= nil then
        self.prefabPath = prefabPath;
    end
    ResMgr:loadPrefab(self.prefabPath, function(gameObject)
        --增加引用计数
        if self.prefabPath and referenceCount[self.prefabPath] then
            referenceCount[self.prefabPath] = referenceCount[self.prefabPath] + 1;
        elseif self.prefabPath then
            referenceCount[self.prefabPath] = 1;
        end
        self.gameObject = gameObject;
        self.isInit = true;
        self:loadPrefabComplete();
    end);
end

function Display:loadPrefabComplete()
    self.rtf = self.gameObject.transform:GetComponent(typeof(RectTransform));
    self:setParent(self.parent);
    self:onStart();
    self.gameObject:SetActive(self.visible);
    if self.visible then
        self:onShow();
        self:onData();
    end
end

function Display:setPrefabPath(prefabPath)
    self.prefabPath = prefabPath;
end

function Display:setParent(parent)
    self.parent = parent;
    if self.isInit and not Utils.isNil(parent) then
        self.gameObject.transform:SetParent(parent.transform, false);
    end
end

function Display:setVisible(visible)
    self.visible = visible;
    if not self.isInit then
        return;
    end
    local activeSelf = self.gameObject.activeSelf;
    self.gameObject:SetActive(visible);
    if visible and not activeSelf then
        self:onShow();
        self:onData();
    elseif not visible and activeSelf then
        self:removeListener();
        self:onHide();
    end
end

function Display:find(path, type)
    if self.isInit then
        local tf = self.gameObject.transform;
        if path then
            tf = self.gameObject.transform:Find(path);
        end
        if type then
            return tf:GetComponent(typeof(type));
        else
            return tf;
        end
    end
end

function Display:setPos(x, y, z)
    if self.isInit then
        if self.rtf then
            self.rtf.anchoredPosition = Vector2(x, y);
        else
            if not z then
                z = 0;
            end
            self.gameObject.transform.localPosition = Vector3(x, y, z);
        end
    end
end

function Display:setRedot(state)
    if self.isInit then
        Utils.addRedotImage(self.gameObject, state);
    end
end

function Display:destroy()
    if self.isInit then
        self:setVisible(false);
        self:onDestroy();
        Destroy(self.gameObject);
        self.gameObjec = nil;
        self.visible = true;
        self.data = nil;
        self.isInit = false;
        self:clearRes();
        --减少贴图资源引用计数
        for i, v in ipairs(self.textureDic) do
            TextureMgr:minusReferenceCount(v.sprite);
            TextureMgr:minusReferenceCount(v.texture);
        end
    end
end

function Display:clearRes()
    if self.module then
        self.module:clearModuleRes();
        return ;
    end
    --减少引用计数
    if not self.prefabPath then
        return ;
    end
    referenceCount[self.prefabPath] = referenceCount[self.prefabPath] - 1;
    local folder = Utils.split(self.prefabPath,"/")[0];
    if folder == "Effect" or folder == "Model" then
        Utils.log("减少引用计数",self.prefabPath,referenceCount[self.prefabPath]);
        if referenceCount[self.prefabPath] == 0 then
            ResMgr:clearRes(self.prefabPath);
        end
    end
end